package com.seekpatent.managed;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component("searchManager")
@Scope("session")
public class SearchManager {
	private String firstLevelKeywords;
	private String secondLevelKeywords;
	private int numberOfHits;

	private StringBuilder urlString;
	List<String> firstLevelKeywordsList;
	List<String> secondLevelKeywordsList;

	public SearchManager() {
	}

	public String getFirstLevelKeywords() {
		return firstLevelKeywords;
	}

	public void setFirstLevelKeywords(String firstLevelKeywords) {
		this.firstLevelKeywords = firstLevelKeywords;
	}

	public String getSecondLevelKeywords() {
		return secondLevelKeywords;
	}

	public void setSecondLevelKeywords(String secondLevelKeywords) {
		this.secondLevelKeywords = secondLevelKeywords;
	}

	public int getNumberOfHits() {
		return numberOfHits;
	}

	public void setNumberOfHits(int numberOfHits) {
		this.numberOfHits = numberOfHits;
	}

	public List<String> getFirstLevelKeywordsList() {
		return firstLevelKeywordsList;
	}

	public void setFirstLevelKeywordsList(List<String> firstLevelKeywordsList) {
		this.firstLevelKeywordsList = firstLevelKeywordsList;
	}

	public List<String> getSecondLevelKeywordsList() {
		return secondLevelKeywordsList;
	}

	public void setSecondLevelKeywordsList(List<String> secondLevelKeywordsList) {
		this.secondLevelKeywordsList = secondLevelKeywordsList;
	}

	private String getQueryUrl() {
		urlString = new StringBuilder(
				"http://patft.uspto.gov/netacgi/nph-Parser?Sect1=PTO2&Sect2=HITOFF&u=%2Fnetahtml%2FPTO%2Fsearch-adv.htm&r=0&p=1&f=S&l=50&Query=+");
		if (firstLevelKeywords != null) {
			String delims = "[,|\\s]";
			String[] firstLevelKeywordsArray = firstLevelKeywords.trim().split(delims);
			firstLevelKeywordsList = Arrays.asList(firstLevelKeywordsArray);
			/*
			 * int numberOfParenthesis = firstLevelKeywordsArray.length; for
			 * (int i=0; i<numberOfParenthesis+1; i++) urlString.append("%28");
			 */
			for (int i = 0; i < firstLevelKeywordsArray.length; i++) {
				if (i == firstLevelKeywordsArray.length - 1)
					urlString.append(firstLevelKeywordsArray[i]);
				else {
					urlString.append(firstLevelKeywordsArray[i]);
					urlString.append("+AND+");
				}
			}
		}
		urlString.append("&d=PTXT");
		System.out.println(urlString.toString());

		return urlString.toString();
	}

	public String getResult() {
		String url = getQueryUrl();
		try {
			Document doc = Jsoup.connect(url).timeout(0).get();
			Elements strongs = doc.select("strong");
			if (strongs.isEmpty())
				return "template1?faces-redirect=true";
			numberOfHits = Integer.parseInt(strongs.get(2).text());
			if (numberOfHits > 5)
				return "template3?faces-redirect=true";
			if (numberOfHits <= 5) {
				Elements TRs = doc.select("TR");
				for (int i = 1; i < TRs.size() - 1; i++) {
					// TD add result to somewhere
				}
				return "template2?faces-redirect=true";
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "about?faces-redirect=true";
	}

}
